﻿using ManaSky.Common.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManaSky.Common.Models
{
    /// <summary>
    /// 接口返回类
    /// </summary>
    public class ApiRequest
    {
        #region 属性
        /// <summary>
        /// 状态码
        /// </summary>
        public ResultCode Code { get; set; }
        /// <summary>
        /// 返回结果内容 成功：Success  失败：异常内容
        /// </summary>
        public string Message { get; set; } = string.Empty;
        /// <summary>
        /// 返回的可选信息
        /// </summary>
        public object? Data { get; set; } = null;
        #endregion

        #region 构造
        /// <summary>
        /// 构造函数 
        /// </summary>
        public ApiRequest()
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="code">消息</param>
        public ApiRequest(ResultCode code)
            : this()
        {
            this.Code = code;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="code">状态码</param>
        /// <param name="message">返回结果内容 成功：Success  失败：异常内容</param>
        public ApiRequest(ResultCode code, string message)
            : this(code)
        {
            this.Message = message;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="code">状态码</param>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        public ApiRequest(ResultCode code, string message, object data)
            : this(code, message)
        {
            this.Data = data;
        }
        #endregion

        #region 方法
        /// <summary>
        /// 构建操作成功信息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public void GetSuccess(object data, string message = "")
        {
            this.Data = data;
            this.Message = message;
            this.Code = ResultCode.Success;
        }

        /// <summary>
        /// 构建操作成功信息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public void GetFail(string msg)
        {
            this.Message = msg;
            this.Code = ResultCode.Fail;
        }

        /// <summary>
        /// 构建操作成功信息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest Success(object data)
        {
            return new ApiRequest(ResultCode.Success, "Success", data);
        }
        /// <summary>
        /// 构建操作失败信息
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest Fail(string msg, object data = null)
        {
            return new ApiRequest(ResultCode.Fail, msg, data);
        }
        /// <summary>
        /// 未登录、登录过期
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest LoginExpired(string msg, object data = null)
        {
            return new ApiRequest(ResultCode.LoginExpired, msg, data);
        }
        /// <summary>
        /// 无权限访问
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest AccessWithoutPermission(string msg, object data = null)
        {
            return new ApiRequest(ResultCode.AccessWithoutPermission, msg, data);
        }
        /// <summary>
        /// 请求不合理，服务器拒绝执行
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest Unauthorized(string msg, object data = null)
        {
            return new ApiRequest(ResultCode.Unauthorized, msg, data);
        }
        /// <summary>
        /// 找不到所需数据
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest NotFound(string msg, object data = null)
        {
            return new ApiRequest(ResultCode.NotFound, msg, data);
        }
        /// <summary>
        /// 服务器内部错误
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ApiRequest InternalServerError(string msg, object data = null)
        {
            return new ApiRequest(ResultCode.InternalServerError, msg, data);
        }
        #endregion
    }
}
